﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace 扩展方法_反射_特性综合练习
{
    //联系要求：   封装OrdeStateEnum枚举类型的扩展方法，实现获取每个枚举值的描述

    //实现如下扩展方法： string desc = OrdeStateEnum.WaitPay.GetDescraption();

    [Description("订单状态枚举")]
    public enum OrderStateEnum
    {
        [Description("待支付")]
        WaitPay,
        [Description("待发货")]
        WaitSend,
        [Description("待收货")]
        WaitReceive,
        [Description("待评论")]
        WaitComment,
        [Description("已完成")]
        Finishi,
        [Description("已取消")]
        Cancel
    }


    /// <summary>
    /// 扩展
    /// </summary>
    /// ①、方法扩展要放在静态类中
    public static class EunmExtend
    {
        //②、在静态方法参数中使用this+ 类型名 ，指定扩展的是哪个类型
        public static string GetDescription(this OrderStateEnum obj)
        {
            var type1 = typeof(OrderStateEnum);
            //获取当前被操作的枚举字段
            var field = type1.GetField(obj.ToString());
            //获取字段的Description特性
            var attribute = field.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute;
            return attribute.Description;
        }
    }

    public class testExtend
    {
        public void test()
        {
            OrderStateEnum enum1 = OrderStateEnum.WaitPay;
            Console.WriteLine(enum1.GetDescription());
            Console.ReadLine();
        }
    }
}
